package com.qf.team2.netdisc.controller;

import com.qf.team2.netdisc.common.base.BaseController;
import com.qf.team2.netdisc.common.pojo.entity.User;
import com.qf.team2.netdisc.common.pojo.qo.UserQO;
import com.qf.team2.netdisc.common.pojo.vo.RespResultVO;
import com.qf.team2.netdisc.common.util.MD5Util;
import com.qf.team2.netdisc.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户登录控制层
 *
 * @author QWER
 */
@SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
@Controller
@RequestMapping("/user")
@Api(tags = "用户业务接口列表")
public class UserController extends BaseController<Object> {
    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping(value = "/login", produces = {"application/json"}, method = RequestMethod.POST)
    @ApiOperation("用户登录接口")
    public RespResultVO<Object> login(@RequestBody UserQO params, HttpServletResponse response) {
        System.out.println("login");
        System.out.println(params);
        String s = MD5Util.md5(params.getPassword());
        params.setPassword(s);
        User dbUser = userService.userLogin(params);

        Cookie cookie = new Cookie("username", dbUser.getUsername());
        cookie.setMaxAge(60 * 60 * 24 * 7);
        cookie.setPath("/");
        response.addCookie(cookie);

        Cookie cookie1 = new Cookie("userId", "" + dbUser.getId());
        cookie1.setMaxAge(60 * 60 * 24 * 7);
        cookie1.setPath("/");
        response.addCookie(cookie1);

        Cookie cookie2 = new Cookie("rootId", "" + dbUser.getRootId());
        cookie2.setMaxAge(60 * 60 * 24 * 7);
        cookie2.setPath("/");
        response.addCookie(cookie2);
        return successResp(dbUser, "登录成功");
    }

    @ResponseBody
    @RequestMapping(value = "/register", produces = {"application/json"}, method = RequestMethod.POST)
    @ApiOperation("用户注册")
    public RespResultVO<Object> register(@RequestBody UserQO params, HttpServletResponse resp) {
        User dbUser = userService.registerByUserNameAndPassword(params);
        String msg = "注册成功";
        return successResp(dbUser, msg);
    }

    @ResponseBody
    @RequestMapping(value = "/capacity", method = RequestMethod.POST)
    @ApiOperation("用户剩余容量接口")
    public RespResultVO<Object> checkCapacity(HttpServletRequest request) {
        String userName = null;
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if ("userName".equals(cookie.getName())) {
                userName = cookie.getValue();
            }
        }
        System.out.println(userName);
        int i = userService.userCapacity(userName);
        System.out.println(i);
        return successResp(RespResultVO.StatusCode.SUCCESS, "" + i);
    }

}
